package com.lp.multi.juc.collection;

import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author: lp
 * @create: 2021-06-27 20:15
 * @description: 阻塞队列
 *
 * 阻塞队列4组api
 *add--remove--element(立即返回、立即报错)    offer--poll--peek(立即返回或指定时间返回)   put--take(一直等待)
 * 集合结构
 *                                                      Collection
 *                         |---------------------------------|--------------------------|
 *                        List                             Queue                      Set
 *      |---------|-----------------------|-----------------|-----------|--------------------|
 *   ArrayList                       Deque     BlockingQueue  AbstractQueue       HashSet
 *                                            |------------|
 *                                  LinkedBlockingQueue  ArrayListBlockingQueue
 *
 * 
 * 
 */
public class BlockingQueueTest1 {
    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<String>(3);

        for (int i = 0; i < 3; i++) {
            new Thread(()->{
                blockingQueue.add(UUID.randomUUID().toString().substring(0,5));
            }).start();
        }
        //不sleep会出现异常:java.util.NoSuchElementException
        TimeUnit.SECONDS.sleep(3);

        for (int i = 0; i < 3; i++) {
            new Thread(()->{
                System.out.println(blockingQueue.remove());
            }).start();
        }
    }
}

class BlockingQueueTest2{
    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<String>(3);

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                //
                boolean isOffer = blockingQueue.offer(UUID.randomUUID().toString().substring(0,5));
                System.out.println(Thread.currentThread().getName()+"添加"+isOffer);
            }).start();
        }
        TimeUnit.SECONDS.sleep(3);
        //立即返回取得结果
        for (int i = 0; i < 11; i++) {
            new Thread(()->{
                System.out.println(blockingQueue.poll());
            }).start();
        }
    }
}

class BlockingQueueTest3{
    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<String>(3);

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                boolean isOffer = false;
                try {
                    isOffer = blockingQueue.offer(UUID.randomUUID().toString().substring(0,5),5, TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"添加"+isOffer);
            }).start();
        }
        TimeUnit.SECONDS.sleep(1);
        //立即返回取得结果
        for (int i = 0; i < 11; i++) {
            new Thread(()->{
                try {
                    System.out.println(blockingQueue.poll(6,TimeUnit.SECONDS));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}


class BlockingQueueTest4{
    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<String>(3);

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                try {
                    blockingQueue.put(UUID.randomUUID().toString().substring(0,5));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        TimeUnit.SECONDS.sleep(1);
        //立即返回取得结果
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                try {
                    System.out.println(blockingQueue.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }).start();
        }
    }
}


